package com.A21.Adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.A21.Object.Global;
import com.A21.Object.TrnDetail;
import com.A21MobileSales.CartActionBarCallBack;
import com.A21MobileSales.OrderDetail;
import com.A21MobileSales.ProductSelection;
import com.A21MobileSales.R;
import com.A21MobileSales.TrnProductDetails;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class TrnDetailAdapter extends BaseAdapter {

	public interface UpdateQuantityListener {
		void onUpdate(boolean isPlus, int index);

	}

	static class ViewHolder {
		private Button btnReduction, btnEdit, btnIncrease;
		private ImageView imgCart, imgCheckTrue;
		private TextView lblPrice, lblNameCart, lblNumberCart, lblTotalPrice,
				lblCodeCart,lblDiscount;
	}

	private TrnDetail cartinfo;
	private String Code;
	private ArrayList<TrnDetail> listCart;
	private static LayoutInflater inflater = null;
	// public ImageLoader imageLoader;
	public Context context;
	public String TAG = "Cart";
	UpdateQuantityListener listener;

	/**
	 * @return the listener
	 */
	public UpdateQuantityListener getListener() {
		return listener;
	}

	/**
	 * @param listener
	 *            the listener to set
	 */
	public void setListener(UpdateQuantityListener listener) {
		this.listener = listener;
	}

	public TrnDetailAdapter(String CustomerCode,Activity activity, ArrayList<TrnDetail> d,
			UpdateQuantityListener listener) {
		listCart = d;
		context = activity;
		Code=CustomerCode;
		this.listener = listener;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listCart.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listCart.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row_trn_detail, null);
			holder = new ViewHolder();
			// holder.imgCart = (ImageView)
			// convertView.findViewById(R.id.imgCart);
			holder.imgCheckTrue = (ImageView) convertView
					.findViewById(R.id.imgCheckTrue);
			holder.btnIncrease = (Button) convertView
					.findViewById(R.id.btnIncrease);
			//holder.btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
			holder.btnReduction = (Button) convertView
					.findViewById(R.id.btnReduction);
			holder.lblNameCart = (TextView) convertView
					.findViewById(R.id.lblNameCart);
			holder.lblCodeCart = (TextView) convertView
					.findViewById(R.id.lblCodeCart);
			holder.lblNumberCart = (TextView) convertView
					.findViewById(R.id.lblNumberCart);
			holder.lblPrice = (TextView) convertView
					.findViewById(R.id.lblPrice);
			holder.lblTotalPrice = (TextView) convertView
					.findViewById(R.id.lblTotalPrice);
			holder.lblDiscount = (TextView) convertView
					.findViewById(R.id.lblDiscount); 
			convertView.setTag(holder);
		}
		final TrnDetail o = listCart.get(position);
		if (o != null) {
			holder = (ViewHolder) convertView.getTag();
			// holder.imgCart.setImageDrawable(AssetUtil.getDrawable(context,
			// o.getImgUrl()));
			holder.lblNameCart.setText(o.ProdDesc);
			holder.lblCodeCart.setText(o.Product);
			/*holder.btnEdit.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					gotoActive(o, TrnProductDetails.class);
				}

			});*/
			holder.lblNameCart.setSelected(true);
			DecimalFormat df1 = new DecimalFormat("#");
			DecimalFormat df2 = new DecimalFormat("#0.00");
			if (o.OrderQuantity < 10) {
				holder.lblNumberCart.setText("0" + df2.format(o.OrderQuantity)
						+ "");
			} else {
				holder.lblNumberCart.setText(df1.format(o.OrderQuantity) + "");
			}
			//holder.lblPrice.setText(df2.format(o.UnitPrice - (o.UnitPrice * o.LineDiscount1)) + "");
			holder.lblPrice.setText(df2.format(o.UnitPrice ) + "");
			final double total =o.OrderQuantity * (o.UnitPrice - (o.UnitPrice * o.LineDiscount1));
			 
			if(o.LineDiscount1>0){
				 holder.lblDiscount.setText("  Less : "+ df2.format(o.LineDiscount1*100) +"%");
				//holder.lblDiscount.setText("U/P :"+ String.format("%-15s",   df2.format(o.UnitPrice)) +"  Less : "+ df2.format(o.LineDiscount1*100) +"%");
			} else{ 
			   holder.lblDiscount.setText("");
			 }
			holder.lblTotalPrice.setText(df2.format(total) + "");
			holder.btnIncrease.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onUpdate(true, position);
				}
			});
			holder.btnReduction.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onUpdate(false, position);
				}
			});
			if (!o.isTrue) {
				holder.imgCheckTrue.setVisibility(View.GONE);
			} else {
				holder.imgCheckTrue.setVisibility(View.VISIBLE);
			}
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (o.isTrue == true) {
						o.isTrue = false;
					} else {
						o.isTrue = true;
					}
					OrderDetail activity = (OrderDetail) context;
					int cntSelected = CountSelected();
					if (cntSelected > 0) {
						activity.mActionMode = activity
								.startActionMode(new CartActionBarCallBack( activity,cntSelected));
						int doneButtonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
						View doneButton = activity.findViewById(doneButtonId);  
						doneButton.setOnClickListener(new View.OnClickListener() { 
						    @Override
						    public void onClick(View v) {
						    	OrderDetail activity = (OrderDetail) context;
						    	activity.ClearSelection();
						    	activity.mActionMode.finish(); 
						    }
						});
					} else {
						activity.mActionMode.finish();
					}
					 
					notifyDataSetChanged();

				}
			});
			convertView.setLongClickable(true);
			convertView.setOnLongClickListener(new View.OnLongClickListener() {
				  @Override
				  public boolean onLongClick(View v) {
					 gotoActive(o, TrnProductDetails.class);
				    return true;
				  }
				});
		} else {
			Log.i(TAG, "Null Object !");
		}

		return convertView;

	}

	private int CountSelected() {
		int selected = 0;
		for (TrnDetail row : listCart) {
			if (row.isTrue) {
				selected++;
			}
		}
		return selected;
	}

	public void gotoActive(TrnDetail cart, Class<?> cls) {
		Intent intent = new Intent(context, cls);
		Bundle bundle = new Bundle();
		bundle.putString("Id", String.valueOf(cart.Id));
		bundle.putString("Product", cart.Product);
	    bundle.putString("Code", Code);
		Global.setSelectedTrnDetail(cart);
		intent.putExtras(bundle);
		Activity activity = (Activity) context;
		activity.startActivityForResult(intent, 1);

	}

}
